import type { FormSchemaGetter } from '#/adapter/form';
import type { VxeGridProps } from '#/adapter/vxe-table';

import { computed } from 'vue';

import { useAccess } from '@vben/access';
import { DictEnum } from '@vben/constants';
import { useUserStore } from '@vben/stores';

import { listUserByDeptId } from '#/api/system/user';
import { calculateAge } from '#/utils/date/date-util';
import { getDictOptions } from '#/utils/dict';
import { renderDict } from '#/utils/render';

const { hasAccessByRoles } = useAccess();
const isAdmin = computed(() => {
  return hasAccessByRoles(['admin', 'superadmin', 'ceo']);
});

const isStoreManage = computed(() => {
  return hasAccessByRoles([
    'shop-owner',
    'sy-wfl-shop-owner',
    'hs-smy-shop-owner',
    'nqf-zd-dc-shop-owner',
  ]);
});

const userStore = useUserStore();

export const querySchema: FormSchemaGetter = () => [
  {
    component: 'Input',
    fieldName: 'customerName',
    label: '客户名称',
  },
  {
    component: 'Input',
    fieldName: 'phone',
    label: '电话',
  },
  {
    component: 'Input',
    fieldName: 'takingProductsDesc',
    label: '服用产品',
  },
  {
    component: 'Select',
    componentProps: {
      options: getDictOptions(DictEnum.SYS_CUSTOMER_TYPE),
    },
    fieldName: 'customerType',
    label: '客户类型',
  },
  {
    component: 'Select',
    fieldName: 'storeId',
    label: '服务店铺',
    dependencies: {
      show: () => isAdmin.value,
      triggerFields: [''],
    },
  },
  {
    component: 'Select',
    fieldName: 'staffId',
    label: '服务员工',
    dependencies: {
      async componentProps(values) {
        if (!isAdmin.value || values?.storeId) {
          const storeId = isAdmin.value
            ? values?.storeId
            : userStore.userInfo?.deptId;
          const ret = await listUserByDeptId(storeId);
          const transOptions = ret.map((item) => ({
            label: item.userName,
            value: item.userId,
          }));
          const dataSourceOptions = [];
          dataSourceOptions.push(...transOptions);
          return {
            options: dataSourceOptions,
          };
        }
        return { allowClear: true };
      },
      show: () => isAdmin.value || isStoreManage.value,
      triggerFields: ['storeId'],
    },
  },

  {
    component: 'Select',
    fieldName: 'memberLevel',
    label: '会员等级',
    componentProps: {
      options: getDictOptions(DictEnum.SYS_MEMBER_LEVEL),
    },
  },
];

export const columns: VxeGridProps['columns'] = [
  { type: 'checkbox', width: 60, fixed: 'left' },
  {
    title: '客户名称',
    field: 'customerName',
    width: 100,
    fixed: 'left',
  },
  {
    title: '客户性别',
    field: 'customerSex',
    width: 100,
    slots: {
      default: ({ row }) => {
        if (row.customerSex) {
          return renderDict(row.customerSex, DictEnum.SYS_USER_SEX);
        }
        return '-';
      },
    },
  },
  {
    title: '出生年月',
    field: 'customerBirth',
    width: 100,
  },
  {
    title: '客户年龄',
    field: 'customerAge',
    width: 100,
    slots: {
      default: ({ row }) => {
        if (row.customerBirth) {
          return `${calculateAge(row.customerBirth)}`;
        }
        return '-';
      },
    },
  },
  {
    title: '电话',
    field: 'phone',
    width: 150,
    fixed: 'left',
  },
  {
    title: '生日类型',
    field: 'birthdayType',
    width: 100,
    slots: {
      default: ({ row }) => {
        if (row.birthdayType) {
          return renderDict(row.birthdayType, DictEnum.SYS_BRITH_TYPE);
        }
        return '-';
      },
    },
  },
  {
    title: '生日',
    field: 'customerBirthday',
    width: 100,
  },
  {
    title: '政治面貌',
    field: 'political',
    width: 100,
    slots: {
      default: ({ row }) => {
        if (row.political) {
          return renderDict(row.political, DictEnum.SYS_POLITICAL);
        }
        return '-';
      },
    },
  },
  {
    title: '籍贯',
    field: 'nativePlace',
    width: 100,
  },
  {
    title: '现住址',
    field: 'address',
    width: 100,
  },
  {
    title: '兴趣爱好',
    field: 'hobby',
    width: 100,
  },
  {
    title: '特长',
    field: 'specially',
    width: 100,
  },
  {
    title: '退休单位',
    field: 'retiredUnit',
    width: 100,
  },
  {
    title: '职称',
    field: 'title',
    width: 100,
  },
  {
    title: '结婚日类型',
    field: 'weddingDayType',
    width: 100,
    slots: {
      default: ({ row }) => {
        if (row.weddingDayType) {
          return renderDict(row.weddingDayType, DictEnum.SYS_POLITICAL);
        }
        return '-';
      },
    },
  },
  {
    title: '结婚日',
    field: 'weddingDay',
    width: 100,
  },
  {
    title: '是否做主',
    field: 'isBoss',
    width: 100,
    slots: {
      default: ({ row }) => {
        if (row.isBoss) {
          return renderDict(row.isBoss, DictEnum.SYS_YES_NO);
        }
        return '-';
      },
    },
  },
  {
    title: '退休金',
    field: 'pension',
    width: 100,
  },
  {
    title: '既往病史',
    field: 'medicalHistory',
    width: 100,
  },
  {
    title: '身体状况',
    field: 'physicalCondition',
    width: 100,
  },
  {
    title: '是否养生保健',
    field: 'isHealthCare',
    width: 120,
    slots: {
      default: ({ row }) => {
        if (row.isHealthCare) {
          return renderDict(row.isHealthCare, DictEnum.SYS_YES_NO);
        }
        return '-';
      },
    },
  },
  {
    title: '服用产品',
    field: 'takingProductsDesc',
    width: 100,
  },
  {
    title: '为什么购买',
    field: 'whyBuy',
    width: 100,
  },
  {
    title: '购买渠道',
    field: 'buyMethod',
    width: 100,
    slots: {
      default: ({ row }) => {
        if (row.customerType) {
          return renderDict(row.customerType, DictEnum.SYS_BUY_METHOD);
        }
        return '-';
      },
    },
  },
  {
    title: '购买人',
    field: 'buyer',
    width: 100,
  },
  {
    title: '积分币',
    field: 'pointsCoin',
    width: 100,
  },
  {
    title: '会员等级',
    field: 'memberLevel',
    width: 100,
    slots: {
      default: ({ row }) => {
        if (row.memberLevel) {
          return renderDict(row.memberLevel, DictEnum.SYS_MEMBER_LEVEL);
        }
        return '-';
      },
    },
  },
  {
    title: '客户类型',
    field: 'customerType',
    width: 100,
    slots: {
      default: ({ row }) => {
        if (row.customerType) {
          return renderDict(row.customerType, DictEnum.SYS_CUSTOMER_TYPE);
        }
        return '-';
      },
    },
  },
  {
    title: '客户活跃度',
    field: 'customerActivity',
    width: 100,
    slots: {
      default: ({ row }) => {
        if (row.customerActivity) {
          return renderDict(
            row.customerActivity,
            DictEnum.SYS_CUSTOMER_ACTIVITY,
          );
        }
        return '-';
      },
    },
  },
  {
    title: '服务员工',
    field: 'staffName',
    width: 100,
  },
  {
    title: '服务店铺',
    field: 'storeName',
    fixed: 'right',
    width: 100,
  },
  {
    title: '备注',
    field: 'remark',
    width: 100,
  },
  {
    field: 'action',
    fixed: 'right',
    slots: { default: 'action' },
    title: '操作',
    width: 180,
  },
];

export const modalSchema: FormSchemaGetter = () => [
  {
    label: '客户id',
    fieldName: 'customerId',
    component: 'Input',
    dependencies: {
      show: () => false,
      triggerFields: [''],
    },
  },
  {
    label: '客户名称',
    fieldName: 'customerName',
    component: 'Input',
    rules: 'required',
    componentProps: {
      placeholder: '请输入客户名称',
      maxlength: 100,
      showCount: true,
    },
  },
  {
    label: '客户性别',
    fieldName: 'customerSex',
    component: 'Select',
    componentProps: {
      options: getDictOptions(DictEnum.SYS_USER_SEX),
    },
  },
  {
    label: '出生日期',
    fieldName: 'customerBirth',
    component: 'DatePicker',
    componentProps: {
      showTime: true,
      format: 'YYYY/MM/DD',
      valueFormat: 'YYYY/MM/DD',
    },
    rules: 'required',
  },
  {
    label: '客户年龄',
    fieldName: 'customerAge',
    component: 'InputNumber',
    rules: 'required',
    dependencies: {
      componentProps(values) {
        if (values?.customerBirth) {
          // 获得年龄
          values.customerAge = calculateAge(values.customerBirth);
        }
        return {};
      },
      show: () => false,
      triggerFields: ['customerBirth'],
    },
  },
  {
    label: '生日类型',
    fieldName: 'birthdayType',
    component: 'Select',
    componentProps: {
      options: getDictOptions(DictEnum.SYS_BRITH_TYPE),
    },
  },
  {
    label: '客户生日',
    fieldName: 'customerBirthday',
    component: 'DatePicker',
    componentProps: {
      showTime: true,
      format: 'MM月DD日',
      valueFormat: 'MM月DD日',
    },
  },
  {
    label: '政治面貌',
    fieldName: 'political',
    component: 'Select',
    componentProps: {
      options: getDictOptions(DictEnum.SYS_POLITICAL),
    },
  },
  {
    label: '籍贯',
    fieldName: 'nativePlace',
    component: 'Input',
    componentProps: {
      placeholder: '请输入籍贯',
      showCount: true,
      maxlength: 100,
    },
  },
  {
    label: '现住址',
    fieldName: 'address',
    component: 'Textarea',
    componentProps: {
      showCount: true,
      maxlength: 100,
      placeholder: '请输入现住址',
      autoHeight: { minRows: 3, maxRows: 5 },
    },
  },
  {
    label: '兴趣爱好',
    fieldName: 'hobby',
    component: 'Input',
    componentProps: {
      placeholder: '请输入兴趣爱好',
      showCount: true,
      maxlength: 100,
    },
  },
  {
    label: '特长',
    fieldName: 'specially',
    component: 'Input',
    componentProps: {
      placeholder: '请输入特长',
      showCount: true,
      maxlength: 100,
    },
  },
  {
    label: '退休单位',
    fieldName: 'retiredUnit',
    component: 'Input',
    componentProps: {
      placeholder: '请输入退休单位',
      showCount: true,
      maxlength: 100,
    },
  },
  {
    label: '职称',
    fieldName: 'title',
    component: 'Input',
    componentProps: {
      placeholder: '请输入职称',
      showCount: true,
      maxlength: 100,
    },
  },
  {
    label: '结婚日类型',
    fieldName: 'weddingDayType',
    component: 'Select',
    componentProps: {
      options: getDictOptions(DictEnum.SYS_BRITH_TYPE),
    },
  },
  {
    label: '结婚日',
    fieldName: 'weddingDay',
    component: 'DatePicker',
    componentProps: {
      showTime: true,
      format: 'YYYY-MM-DD',
      valueFormat: 'YYYY-MM-DD',
    },
  },
  {
    label: '电话',
    fieldName: 'phone',
    component: 'Input',
    rules: 'required',
  },
  {
    label: '是否做主',
    fieldName: 'isBoss',
    component: 'Select',
    componentProps: {
      options: getDictOptions(DictEnum.SYS_YES_NO),
    },
    defaultValue: 'Y',
  },
  {
    label: '退休金',
    fieldName: 'pension',
    component: 'InputNumber',
    renderComponentContent: () => ({
      prefix: () => '￥',
      suffix: () => 'RMB',
    }),
  },
  {
    label: '既往病史',
    fieldName: 'medicalHistory',
    component: 'Textarea',
    componentProps: {
      showCount: true,
      maxlength: 100,
      placeholder: '请输入既往病史',
      autoHeight: { minRows: 3, maxRows: 5 },
    },
  },
  {
    label: '身体状况',
    fieldName: 'physicalCondition',
    component: 'Input',
    componentProps: {
      placeholder: '请输入身体状况',
      showCount: true,
      maxlength: 100,
    },
  },
  {
    label: '是否养生保健',
    fieldName: 'isHealthCare',
    component: 'Select',
    componentProps: {
      options: getDictOptions(DictEnum.SYS_YES_NO),
    },
    defaultValue: 'Y',
  },
  {
    label: '服用产品',
    fieldName: 'takingProducts',
    component: 'Input',
    // rules: 'required',
    dependencies: {
      show: () => false,
      triggerFields: [''],
    },
  },
  {
    label: '服用产品',
    fieldName: 'takingProductsDesc',
    component: 'Textarea',
    componentProps: {
      showCount: true,
      maxlength: 100,
      placeholder: '请输入服用产品',
      autoHeight: { minRows: 3, maxRows: 5 },
    },
    // rules: 'required',
  },
  {
    label: '为什么购买',
    fieldName: 'whyBuy',
    component: 'Input',
    componentProps: {
      showCount: true,
      maxlength: 50,
      placeholder: '请输入为什么购买',
    },
  },
  {
    label: '购买渠道',
    fieldName: 'buyMethod',
    component: 'Select',
    componentProps: {
      options: getDictOptions(DictEnum.SYS_BUY_METHOD),
    },
  },
  {
    label: '购买人',
    fieldName: 'buyer',
    component: 'Input',
    componentProps: {
      showCount: true,
      maxlength: 50,
      placeholder: '购买人',
    },
  },
  {
    label: '积分币',
    fieldName: 'pointsCoin',
    component: 'InputNumber',
    dependencies: {
      show: () => false,
      triggerFields: [''],
    },
  },
  {
    label: '积分标识',
    fieldName: 'pointsCoinId',
    component: 'InputNumber',
    dependencies: {
      show: () => false,
      triggerFields: [''],
    },
  },
  {
    label: '会员等级',
    fieldName: 'memberLevel',
    component: 'Select',
    rules: 'selectRequired',
    componentProps: {
      options: getDictOptions(DictEnum.SYS_MEMBER_LEVEL),
    },
  },
  {
    label: '所在家庭组',
    fieldName: 'familyId',
    component: 'Select',
  },
  {
    label: '家庭组身份',
    fieldName: 'familyType',
    component: 'RadioGroup',
    componentProps: {
      options: getDictOptions(DictEnum.SYS_FAMILY_TYPE),
      buttonStyle: 'solid',
      optionType: 'button',
    },
    dependencies: {
      show: (values) => values.familyId !== undefined,
      triggerFields: ['familyId'],
    },
  },
  {
    label: '客户类型',
    fieldName: 'customerType',
    component: 'Select',
    componentProps: {
      options: getDictOptions(DictEnum.SYS_CUSTOMER_TYPE),
    },
    rules: 'selectRequired',
  },
  {
    label: '客户活跃度',
    fieldName: 'customerActivity',
    component: 'Select',
    rules: 'required',
    dependencies: {
      show: (values) => [1, 2, 3].includes(Number(values?.customerType)),
      triggerFields: ['customerType'],
      componentProps(values) {
        const type = Number(values?.customerType);
        const allOptions = getDictOptions(DictEnum.SYS_CUSTOMER_ACTIVITY);
        // 主产品-> 在服 停用
        if ([2, 3].includes(type)) {
          return {
            options: allOptions.filter((opt) =>
              [1, 2].includes(Number(opt.value)),
            ),
          };
        }
        // 团购-> 活跃 休眠
        if ([1].includes(type)) {
          return {
            options: allOptions.filter((opt) =>
              [3, 4].includes(Number(opt.value)),
            ),
          };
        }
        return { options: [] };
      },
    },
  },
  {
    label: '服务店铺',
    fieldName: 'storeName',
    component: 'Input',
    rules: 'required',
    dependencies: {
      show: () => false,
      triggerFields: [''],
    },
  },
  {
    label: '服务店铺',
    fieldName: 'storeId',
    component: 'Select',
    rules: 'required',
    defaultValue: userStore.userInfo?.deptId,
    dependencies: {
      // 全程禁用
      show: () => isAdmin.value,
      // 设置默认值为当前登录用户店铺ID
      triggerFields: [''],
    },
  },
  {
    label: '服务员工',
    fieldName: 'staffId',
    component: 'Select',
    rules: 'required',
    defaultValue: userStore.userInfo?.userId,
    dependencies: {
      async componentProps(values) {
        if (isAdmin.value && isStoreManage.value) {
          values.staffId = undefined;
        }
        if (values?.storeId) {
          const ret = await listUserByDeptId(values?.storeId);
          const transOptions = ret.map((item) => ({
            label: item.userName,
            value: item.userId,
          }));
          const dataSourceOptions = [];
          dataSourceOptions.push(...transOptions);
          return {
            options: dataSourceOptions,
          };
        }
        return {};
      },
      // 全程禁用
      show: () => isAdmin.value || isStoreManage.value,
      triggerFields: ['storeId', ''],
    },
  },
  {
    label: '服务员工名称',
    fieldName: 'staffName',
    component: 'Input',
    rules: 'required',
    dependencies: {
      show: () => false,
      triggerFields: [''],
    },
  },
  {
    label: '状态',
    fieldName: 'status',
    component: 'RadioGroup',
    componentProps: {
      buttonStyle: 'solid',
      optionType: 'button',
    },
    dependencies: {
      show: () => false,
      triggerFields: [''],
    },
  },
  {
    label: '备注',
    fieldName: 'remark',
    component: 'Textarea',
    componentProps: {
      showCount: true,
      maxlength: 200,
      placeholder: '请输入备注',
      autoHeight: { minRows: 3, maxRows: 5 },
    },
  },
];
